package com.xindaoapp.happypet.viewpager.windows;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.MyPostAdapter;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.Message;
import com.xindaoapp.happypet.bean.MyHuiFu;
import com.xindaoapp.happypet.ui.PostDetailActivity;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.pulltorefresh.PullToRefreshBase;
import com.xindaoapp.happypet.view.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import com.xindaoapp.happypet.view.pulltorefresh.PullToRefreshListView;
import com.xindaoapp.happypet.viewpager.ViewPagerWindow;

/**
 * 用户我的帖子
 * 
 * @author chenghao
 * 
 */
public class UserMyPostWindow extends ViewPagerWindow {
    private final String uid;
    private PullToRefreshListView listView;

    public UserMyPostWindow(Activity context, String uid) {
        super(context);
        this.uid = uid;
    }

    @Override
    protected View onCreateView() {
        return View.inflate(mContext, R.layout.fragment_other_tab2, null);
    }

    @Override
    protected void initViews() {
        super.initViews();
        initUi(getView());
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        loadData(currentpage);
    }

    private int currentpage = 1;

    protected void initUi(View view) {

        listView = (PullToRefreshListView) view.findViewById(R.id.listview);

        listView.setPullRefreshEnabled(false);
        listView.setScrollLoadEnabled(true);

        listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentpage++;
                loadData(currentpage);
            }
        });
        listView.getRefreshableView().setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, PostDetailActivity.class);
                intent.putExtra(Constants.KEY_POST_TID,
                        ((Message) parent.getAdapter().getItem(position)).tid);
                intent.putExtra(Constants.KEY_FROM_PAGE, 0);
                mContext.startActivity(intent);
            }

        });
        listView.setOnScrollListener(new OnScrollListener() {
            boolean is = false;

            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                // TODO Auto-generated method stub
                if (firstVisibleItem >= 3) {
                    is = true;
                } else {
                    is = false;
                }

                if (is) {

                    Intent intent = new Intent(Constants.SCROLL_FOR);
                    intent.putExtra("go", "up");
                    intent.putExtra("from", "1");
                    getBaseContext().sendBroadcast(intent);
                } else {
                    Intent intent = new Intent(Constants.SCROLL_FOR);
                    intent.putExtra("go", "down");
                    intent.putExtra("from", "1");
                    getBaseContext().sendBroadcast(intent);
                }


            }
        });
    }

    // private final LinkedList<Message> arrays = new LinkedList<Message>();
    private MyPostAdapter adapter;

    // private String total;

    private void loadData(final int currentPage) {
        RequestParams params = new RequestParams();
        params.put("uid", uid);
        params.put("page", String.valueOf(currentPage));
        params.put("pagesize", Address.PAGESIZE);
        AddClient.post(Address.FABIAO, params, new AsyncHttpResponseHandler() {
            // private List<Message> array;

            @Override
            public void onSuccess(int statusCode, String content) {
                super.onSuccess(statusCode, content);
                try {
                    MyHuiFu huifu = JSON.parseObject(content, MyHuiFu.class);
                    if (huifu.array == null || huifu.array.size() == 0) {
                        onNoDataArrived();
                        return;
                    }
                    if (adapter == null) {
                        adapter = new MyPostAdapter(huifu.array, mContext);
                        listView.getRefreshableView().setAdapter(adapter);
                    } else {
                        adapter.addArrays(huifu.array);
                        adapter.notifyDataSetChanged();
                    }
                    if (Integer.parseInt(huifu.getTotal()) < 10) {
                        listView.setHasMoreData(false);
                        listView.setPullLoadEnabled(false);
                        listView.setPullRefreshEnabled(false);
                    }
                    onDataArrived(true);
                    onLoaded();
                } catch (Exception e) {
                }
            }

            @Override
            public void onFailure(Throwable error, String content) {
                Toast.makeText(mContext, "加载失败,请检查网络状态", Toast.LENGTH_SHORT).show();
                super.onFailure(error, content);
                onLoaded();
            }
        });
    }

    private void onLoaded() {
        listView.onPullDownRefreshComplete();
        listView.onPullUpRefreshComplete();
    }


}
